package com.bango.testapp;

import java.util.Hashtable;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bango.android.BangoAgent;

public class AndroidApplicationTestApp extends Activity implements OnClickListener
{
	/** Called when the activity is first created. */
	private static final String TAG = "AndroidApplicationTestApp";

	/** Called when the activity is first created. */
	private Button btnLog;
	private Button btnError;
	private Button btnPageView;
	private Button btnReset;
	private Button btnClear;
	private Button btnExit;
	private Button btnPostNow;
	private Button btnViewBatch;

	private Button btnSearch;
	private Button btnLiveVideoStart;
	private Button btnLiveVideoStop;
	private Button btnOnDemandVideoStart;
	private Button btnOnDemandVideoStop;
	private Button btnIdle;
	private Button btnResume;

	private EditText txtEventName;
	private EditText txtEventDetail;
	private EditText txtEventValue;
	private EditText txtParamName1;
	private EditText txtParamValue1;
	private EditText txtParamName2;
	private EditText txtParamValue2;

	@Override public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.btnLog = (Button) this.findViewById(R.id.btnLog);
		this.btnError = (Button) this.findViewById(R.id.btnError);
		this.btnPageView = (Button) this.findViewById(R.id.btnPageView);
		this.btnReset = (Button) this.findViewById(R.id.btnReset);
		this.btnClear = (Button) this.findViewById(R.id.btnClear);
		this.btnExit = (Button) this.findViewById(R.id.btnExit);
		this.btnPostNow = (Button) this.findViewById(R.id.btnPostNow);
		this.btnViewBatch = (Button) this.findViewById(R.id.btnViewBatch);

		this.btnSearch = (Button) this.findViewById(R.id.btnSearch);
		this.btnLiveVideoStart = (Button) this.findViewById(R.id.btnLiveVideoStart);
		this.btnLiveVideoStop = (Button) this.findViewById(R.id.btnLiveVideoStop);
		this.btnOnDemandVideoStart = (Button) this.findViewById(R.id.btnOnDemandVideoStart);
		this.btnOnDemandVideoStop = (Button) this.findViewById(R.id.btnOnDemandVideoStop);
		this.btnIdle = (Button) this.findViewById(R.id.btnIdle);
		this.btnResume = (Button) this.findViewById(R.id.btnResume);

		this.txtEventName = (EditText) this.findViewById(R.id.txtEventName);
		this.txtEventDetail = (EditText) this.findViewById(R.id.txtEventDetail);
		this.txtEventValue = (EditText) this.findViewById(R.id.txtEventValue);
		this.txtParamName1 = (EditText) this.findViewById(R.id.txtParamName1);
		this.txtParamValue1 = (EditText) this.findViewById(R.id.txtParamValue1);
		this.txtParamName2 = (EditText) this.findViewById(R.id.txtParamName2);
		this.txtParamValue2 = (EditText) this.findViewById(R.id.txtParamValue2);

		this.btnLog.setOnClickListener(this);
		this.btnError.setOnClickListener(this);
		this.btnPageView.setOnClickListener(this);
		this.btnReset.setOnClickListener(this);
		this.btnClear.setOnClickListener(this);
		this.btnExit.setOnClickListener(this);
		this.btnPostNow.setOnClickListener(this);
		this.btnViewBatch.setOnClickListener(this);

		this.btnSearch.setOnClickListener(this);
		this.btnLiveVideoStart.setOnClickListener(this);
		this.btnLiveVideoStop.setOnClickListener(this);
		this.btnOnDemandVideoStart.setOnClickListener(this);
		this.btnOnDemandVideoStop.setOnClickListener(this);
		this.btnIdle.setOnClickListener(this);
		this.btnResume.setOnClickListener(this);

		if (savedInstanceState == null)
		{
			Reset();

			// Set Configuration Options here
			
			// Call start session here
			// BangoAgent.onStartSession(this, [Your Application ID]);
		}

	}

	@Override public void onDestroy()
	{
		if (isFinishing())
		{
			BangoAgent.onEndSession(this);
		}
		super.onDestroy();
	}


	@Override public void onPause()
	{
		super.onPause();
	}

	@Override public void onResume()
	{
		super.onResume();
	}
	@Override public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btnLog:
				Log();
				break;
			case R.id.btnError:
				Error();
				break;
			case R.id.btnPageView:
				PageView();
				break;
			case R.id.btnClear:
				Clear();
				break;
			case R.id.btnReset:
				Reset();
				break;
			case R.id.btnExit:
				Exit();
				break;
			case R.id.btnPostNow:
				PostNow();
				break;
			case R.id.btnSearch:
				Search();
				break;
			case R.id.btnLiveVideoStart:
				LiveVideoStart();
				break;
			case R.id.btnLiveVideoStop:
				LiveVideoStop();
				break;
			case R.id.btnOnDemandVideoStart:
				OnDemandVideoStart();
				break;
			case R.id.btnOnDemandVideoStop:
				OnDemandVideoStop();
				break;
			case R.id.btnIdle:
				Idle();
				break;
			case R.id.btnResume:
				Resume();
				break;
			case R.id.btnViewBatch:
				ViewBatch();
				break;
		}

	}
	
	private void Log()
	{
		
		switch (getOverload()) {
		case 1:
			BangoAgent.onEvent(EventName());
			break;
		case 2:
			BangoAgent.onEvent(EventName(), EventValue());
			break;
		case 3:
			BangoAgent.onEvent(EventName(), EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onEvent(EventName(), EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onEvent(EventName(), EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		ShowMessage("Log Made");
	}

	private void OnDemandVideoStart()
	{
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onDemandVideoStart();
			break;
		case 2:
			BangoAgent.onDemandVideoStart(EventValue());
			break;
		case 3:
			BangoAgent.onDemandVideoStart(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onDemandVideoStart(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onDemandVideoStart(EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		ShowMessage("onDemandVideoStart");
	}

	private void OnDemandVideoStop()
	{
		switch (getOverload()) {
		case 1:
			BangoAgent.onDemandVideoStop();
			break;
		case 2:
			BangoAgent.onDemandVideoStop(EventValue());
			break;
		case 3:
			BangoAgent.onDemandVideoStop(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onDemandVideoStop(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onDemandVideoStop(EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		ShowMessage("onDemandVideoStop");
	}

	private void LiveVideoStart()
	{
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onLiveVideoStart();
			break;
		case 2:
			BangoAgent.onLiveVideoStart(EventValue());
			break;
		case 3:
			BangoAgent.onLiveVideoStart(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onLiveVideoStart(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onLiveVideoStart(EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		ShowMessage("onLiveVideoStart");
	}

	private void LiveVideoStop()
	{	
		switch (getOverload()) {
		case 1:
			BangoAgent.onLiveVideoStop();
			break;
		case 2:
			BangoAgent.onLiveVideoStop(EventValue());
			break;
		case 3:
			BangoAgent.onLiveVideoStop(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onLiveVideoStop(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onLiveVideoStop(EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		ShowMessage("onLiveVideoStop");
	}

	private void Search()
	{
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onSearch();
			break;
		case 2:
			BangoAgent.onSearch(EventValue());
			break;
		case 3:
			BangoAgent.onSearch(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onSearch(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onSearch(EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		ShowMessage("onSearch");
	}

	private void Idle()
	{
	
		switch (getOverload()) {
		case 1:
			BangoAgent.onIdle();
			break;
		case 2:
			BangoAgent.onIdle(EventValue());
			break;
		case 3:
			BangoAgent.onIdle(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onIdle(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onIdle(EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		ShowMessage("onIdle");
	}

	private void Resume()
	{
		switch (getOverload()) {
		case 1:
			BangoAgent.onResume();
			break;
		case 2:
			BangoAgent.onResume(EventValue());
			break;
		case 3:
			BangoAgent.onResume(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onResume(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onResume(EventValue(), EventDetail(),
					EventParameters());
			break;
		}

		ShowMessage("onResume");
	}

	private void PageView()
	{
	
		switch (getOverload()) {
		
		case 1:
			BangoAgent.onPageView();
			break;
		case 2:
			BangoAgent.onPageView(EventValue());
			break;
		case 3:
			BangoAgent.onPageView(EventValue(), EventDetail());
			break;
		case 4:
			BangoAgent.onPageView(EventValue(), EventParameters());
			break;
		default:
			BangoAgent.onPageView(EventValue(), EventDetail(),
					EventParameters());
			break;
		}
		
		ShowMessage("Page View Updated");
	}

	private void Error()
	{
		BangoAgent.onError("TestError", "This is my test error", TAG);
		ShowMessage("Error Posted");
	}

	private void ViewBatch() {
		android.util.Log.d(TAG, "\n---Start Batch---\n");
		android.util.Log.d(TAG, BangoAgent.viewBatch());
		android.util.Log.d(TAG, "\n---End Batch---");
	}

	private void Reset()
	{
		txtEventName.setText("AND Event");
		txtEventDetail.setText("AND Event Detail");
		txtEventValue.setText("AND Event Value");
		txtParamName1.setText("BGO_SOURCE");
		txtParamValue1.setText("AND SOURCE");
		txtParamName2.setText("BGO_TERM");
		txtParamValue2.setText("AND TERM");
	}

	private void Clear()
	{
		txtEventName.setText("");
		txtEventDetail.setText("");
		txtEventValue.setText("");
		txtParamName1.setText("");
		txtParamValue1.setText("");
		txtParamName2.setText("");
		txtParamValue2.setText("");
	}

	private void Exit()
	{
		this.finish();
	}

	private void PostNow()
	{
		BangoAgent.postEvents();
		ShowMessage("Posted Events");
	}

	private void ShowMessage(String messageToShow)
	{
		Toast.makeText(this, messageToShow, Toast.LENGTH_SHORT).show();
	}
	
	private String EventName() {
		return txtEventName.getText().toString();
	}

	private String EventDetail() {
		return txtEventDetail.getText().toString();
	}

	private String EventValue() {
		return txtEventValue.getText().toString();
	}

	private Hashtable<String, String> EventParameters() {
		Hashtable<String, String> eventParameters;
		eventParameters = new Hashtable<String, String>();

		if (txtParamName1.getText().toString().length() > 0
				|| txtParamName2.getText().toString().length() > 0) {
			if (txtParamName1.getText().toString().length() > 0) {
				eventParameters.put(txtParamName1.getText().toString(),
						txtParamValue1.getText().toString());
			}
			if (txtParamName2.getText().toString().length() > 0) {
				eventParameters.put(txtParamName2.getText().toString(),
						txtParamValue2.getText().toString());
			}
		}

		return eventParameters;
	}
	
	private int getOverload() {
		/*
		 * 1 - Only Event Name Provided
		 * 2 - Event name and value provided
		 * 3 - Event name, value and detail provided
		 * 4 - Event name, value and event parameters provided
		 * 5 - Event name, value, detail and event parameters provided
		 */

		String eventValue = EventValue();
		String eventDetail = EventDetail();
		Hashtable<String,String> eventParameters = EventParameters();

		if(eventValue == null){ eventValue = "";}
		if(eventDetail == null){ eventDetail = "";}

		int rtnValue = 1;

		if ((eventValue.length() <= 0)) {
			return rtnValue;
		}

		if ((eventDetail.length() <= 0) && eventParameters.isEmpty()) {
			rtnValue = 2;
			return rtnValue;
		}

		if ((eventDetail.length() > 0) && eventParameters.isEmpty()) {
			return 3;
		}

		if ((eventDetail.length() <= 0) && !eventParameters.isEmpty()) {
			return 4;
		}

		if ((eventDetail.length() > 0) && !eventParameters.isEmpty()) {
			rtnValue = 5;
			return rtnValue;
		}

		return rtnValue;
	}

}
